package com.keyin.widget;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.keyin.R;

public class KeyinWebviewWidget extends MainWidgetBase {

	private View container;

	private WebView webView;

	public KeyinWebviewWidget(Activity context) {
		super(context);
		
		setBackgroundColor(0xFFFFFFFF);
	}
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			initUI();
		};
	};
	
	private void initUI(){
		container = View.inflate(getContext(), R.layout.v_keyin_webview, null);
		addView(container);
		
		webView = (WebView)container.findViewById(R.id.webview);
		webView.loadUrl("http://www.keyin.cn");
		
		webView.getSettings().setJavaScriptEnabled(true);
		
		webView.setWebChromeClient(new WebChromeClient(){
			public void onProgressChanged(WebView view, int progress) {
				if(progress == 100) {
					progressDialog.dismiss();
				}
			}
		});
		
		progressDialog = ProgressDialog.show(parent, "请稍候", "正在获取数据", true);
		progressDialog.setCancelable(true); 
		progressDialog.setProgressStyle(android.R.style.Theme_Translucent);
		progressDialog.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface d) {
			}
		});
	}

	@Override
	public void onNewIntent(Intent i) {
		if (container == null){
			handler.sendEmptyMessage(0);
		}
	}

}
